import './styles/tailwind.css'

import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import { setupLayouts } from 'virtual:generated-layouts'
import generatedRoutes from 'virtual:generated-pages'
import App from './App.vue'
// import { i18n } from './i18n'
import eventBus from './utils/eventBus';

// 根据窗口宽度动态设置根元素字体大小
function setRootFontSize() {
  const windowWidth = window.innerWidth;
  let rootFontSize = 14; // 默认字体大小

  if (windowWidth >= 1024) {
    // rootFontSize = 18; // 桌面设备
    // 适应大屏、宽屏设备
    rootFontSize = windowWidth / 1024 * 14;
  } else if (windowWidth >= 768) {
    rootFontSize = 16; // 平板设备
  }

  document.documentElement.style.fontSize = rootFontSize + 'px';
}

// 初始化根字体大小
setRootFontSize();

// 监听窗口大小变化
window.addEventListener('resize', setRootFontSize);

const router = createRouter({
  history: createWebHistory(),
  routes: setupLayouts(generatedRoutes),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition; // 返回保存的位置（如浏览器后退/前进时）
    } else {
      return { top: 0 }; // 默认滚动到顶部
    }
  },
})

// 路由跳转时设置标题
router.beforeEach((to, from, next) => {
  const titleKey = to.meta.title as string || 'index';
  // if use i18n
  // const title = i18n.global.t(`title.${titleKey}`);
  const title = titleKey;
  document.title = title;
  next();
});

const app = createApp(App).use(router).use(eventBus);
// if use i18n
// app.use(i18n);
app.mount('#app')
